<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>精准饲喂平台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <!-- <link rel="shortcut icon" href="../logo1.png" /> -->
    <link rel="stylesheet" href="../plugins/layui/css/layui.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../layuiadmin/style/admin.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="../plugins/layui/css/style.css?v=${.Config.basic.version}" media="all">
    <link rel="stylesheet" href="//at.alicdn.com/t/font_2575369_ywz1djtgesp.css" media="all">
    <link rel="stylesheet" href="../plugins/toastr/toastr.min.css?v=${.Config.basic.version}">
    <style>

        body a{outline:none;blr:expression(this.onFocus=this.blur());}
        .layadmin-pagetabs .layui-tab-title li.layui-this, .layadmin-pagetabs .layui-tab-title li:hover {
            background-color: #eaeaea;color:#333;
            border-radius: 0px;
        }
        .layadmin-pagetabs .layui-tab-title li.layui-this::after {
            height: 0px;
        }
        .layui-layout-admin .layui-header .layui-nav .layui-nav-item, .layui-layout-admin .layui-logo {
            right: 0px;
        }
        .layui-logo img {
            display: inline;
            width: 48px;
            min-width: 48px;
            height: 48px;
            margin-top: calc((49px - 50px)/ 2);
            margin-left: -10px;
        }

        .logo-title{
            background: linear-gradient(90deg, #3c7cff,#66d1cf);
            background-clip: border-box;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .layui-nav-itemed > .layui-nav-child {
            display: block;
            padding: 0 !important;
            background-color: transparent !important;
        }

        .layui-nav * {
            /*font-size: 14px;*/
            /*font-size: 16px;*/
            /*font-weight: 500;*/
        }

        .layui-layout-admin .layui-body {
            position: fixed;
            top: 50px;
            bottom: 10px;
        }


        .layui-side-menu .layui-nav .layui-nav-itemed .layui-nav-child a {
            font-size: 13px;
            padding-left: 55px;
        }

        .layui-nav .layui-nav-item a {
            display: block;
            padding: 0 20px;
            /* color: #fff; */
            color: #333;
            transition: all .3s;
            -webkit-transition: all .3s;
        }
        .layui-nav-tree .layui-nav-child dd.layui-this, .layui-nav-tree .layui-nav-child dd.layui-this a, .layui-nav-tree .layui-this, .layui-nav-tree .layui-this > a, .layui-nav-tree .layui-this > a:hover {
            color: #fff !important;
        }
        .layui-nav .layui-nav-item a:hover {
            color: #000 !important;
            /* background: #3c7cff; */
        }
        .layui-nav .layui-this a{
            color: #fff !important;
            /* background: #3c7cff; */
            background: linear-gradient(135deg, #3c7cff 60%, #67d3ce 100%);
        }
        .layui-nav-itemed > a, .layui-nav-tree .layui-nav-title a, .layui-nav-tree .layui-nav-title a:hover {
            color: #666 !important;
        }

        .layui-nav .layui-nav-more
        {
            border-color: #3c7cff transparent transparent;
            border-top-color:#3c7cff;
        }
        .layui-nav-itemed > a .layui-nav-more {
            border-color: transparent transparent #3c7cff;
        }

        li.layui-nav-item.layui-this > a > i{
            color: #fff !important;
        }

        .layui-nav-tree .layui-this,
        .layui-nav-tree .layui-this>a,
        .layui-nav-tree .layui-nav-child dd.layui-this,
        .layui-nav-tree .layui-nav-child dd.layui-this a {
            background: linear-gradient(135deg, #3c7cff 60%, #67d3ce 100%) !important;
        }

    </style>

    <!-- <script>
        /^http(s*):\/\//.test(location.href) || alert('请先部署到 localhost 下再访问');
    </script> -->
</head>

<body class="layui-layout-body">

<div id="LAY_app">
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <!-- 头部区域 -->
            <ul class="layui-nav layui-layout-left">
                <li class="layui-nav-item layadmin-flexible" lay-unselect>
                    <a href="javascript:;" layadmin-event="flexible" title="侧边伸缩">
                        <i class="layui-icon layui-icon-shrink-right" id="LAY_app_flexible"></i>
                    </a>
                </li>
                <!-- <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a href="/Dashboard/Index" target="_self" title="大屏">
                        <i class="layui-icon layui-icon-console"></i>
                    </a>
                </li> -->
                <li class="layui-nav-item" lay-unselect>
                    <a href="javascript:;" layadmin-event="refresh" title="刷新">
                        <i class="layui-icon layui-icon-refresh-3"></i>
                    </a>
                </li>
                <!--<li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <input type="text" placeholder="搜索..." autocomplete="off" class="layui-input layui-input-search"
                           layadmin-event="serach" lay-action="template/search.html?keywords=">
                </li>-->
            </ul>
            <ul class="layui-nav layui-layout-right" lay-filter="layadmin-layout-right">

                
                <li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a href="javascript:;" layadmin-event="fullscreen">
                        <i class="layui-icon layui-icon-screen-full"></i>
                    </a>
                </li>
                <li class="layui-nav-item" lay-unselect>
                    <a href="javascript:;">
                        <img src="../img/user_black.png?v=20250626" class="layui-nav-img">
                        <cite id="username"></cite>
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a lay-href="/UserProfile/Profile">基本资料</a></dd>
                        <dd><a lay-href="/UserProfile/Pwd">修改密码</a></dd>
                        <hr>
                        <dd layadmin-event="logout" style="text-align: center;"><a>退出</a></dd>
                    </dl>
                </li>

                <!--<li class="layui-nav-item layui-hide-xs" lay-unselect>
                    <a href="javascript:;" layadmin-event="about"><i class="layui-icon layui-icon-more-vertical"></i></a>
                </li>
                <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-unselect>
                    <a href="javascript:;" layadmin-event="more"><i class="layui-icon layui-icon-more-vertical"></i></a>
                </li>-->
            </ul>
        </div>

        <!-- 侧边菜单 -->
        <div class="layui-side layui-side-menu">
            <div class="layui-side-scroll">
                <div class="layui-logo" lay-href="">
                    <!-- <img src="../logo1.png"> -->
                    <span class="logo-title">精准饲喂平台</span>
                </div>

                <ul class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu" lay-filter="layadmin-system-side-menu">

                </ul>
            </div>
        </div>

        <!-- 页面标签 -->
        <div class="layadmin-pagetabs" id="LAY_app_tabs">
            <div class="iconfont layadmin-tabs-control icon-left layui-icon-prev layui-text-primary" layadmin-event="leftPage"></div>
            <div class="iconfont layadmin-tabs-control icon-right layui-icon-next layui-text-primary" layadmin-event="rightPage"></div>
            <div class="iconfont layadmin-tabs-control icon-expand layui-icon-down layui-text-primary">
                <ul class="layui-nav layadmin-tabs-select" lay-filter="layadmin-pagetabs-nav">
                    <li class="layui-nav-item" lay-unselect>
                        <a href="javascript:;"></a>
                        <dl class="layui-nav-child layui-anim-fadein">
                            <dd layadmin-event="closeThisTabs"><a href="javascript:;">关闭当前标签页</a></dd>
                            <dd layadmin-event="closeOtherTabs"><a href="javascript:;">关闭其它标签页</a></dd>
                            <dd layadmin-event="closeAllTabs"><a href="javascript:;">关闭全部标签页</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
            <div class="layui-tab" lay-unauto lay-allowClose="true" lay-filter="layadmin-layout-tabs">
                <ul class="layui-tab-title" id="LAY_app_tabsheader">
                    <!--<li lay-id="/Dashboard/Index" lay-attr="/Dashboard/Index" class="layui-this"><i class="layui-icon layui-icon-home"></i></li>-->
                    <li lay-id="../Home/Dashboard" lay-attr="/Home/Dashboard" class="layui-this"></li>
                </ul>
            </div>
        </div>


        <!-- 主体内容 -->
        <div class="layui-body" id="LAY_app_body">
            <div class="layadmin-tabsbody-item layui-show">
                <!--<iframe src="//Dashboard/Index" frameborder="0" class="layadmin-iframe"></iframe>-->
                <iframe id="main-iframe" src="" frameborder="0" class="layadmin-iframe" allowfullscreen="true" webkitallowfullscreen="true" mozallowfullscreen="true"></iframe>
            </div>
        </div>

        <!-- 辅助元素，一般用于移动设备下遮罩 -->
        <div class="layadmin-body-shade" layadmin-event="shade"></div>
    </div>
</div>

<script src="../plugins/layui/layui.js?v=${.Config.basic.version}"></script>
<script src="../plugins/jquery/jquery-1.10.2.js?v=${.Config.basic.version}"></script>
<script src="../plugins/toastr/toastr.min.js?v=${.Config.basic.version}"></script>
<script src="../plugins/jquery.slimscroll.min.js?v=${.Config.basic.version}"></script>
<script src="../plugins/moment.min.js"></script>
<script src="../js/utils.js?v=${.Config.basic.version}"></script>
<script src="../js/watermark.js?v=${.Config.basic.version}"></script>

<script>
    var $=null;
    var layer=null;
    var token=null;

    localStorage.removeItem("layuiAdmin");
    localStorage.removeItem("layuiAdmin20250611");
    window.localStorage.setItem("screenTimeout","0");

    layui.config({
        base: '../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','layer'],function(){
        //do something
        layer = layui.layer;
        $ = layui.$;
        //layer.msg('加载完成');
        token = window.localStorage.getItem("token");
        token = 'Bearer ' + token;
        //var userid = window.localStorage.getItem("userid");
        //console.info(token);
        //layer.msg(token);
        initToastr();




        $.ajax({
            type: 'GET',
            url: '../Api/Menu',
            data: {},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (data) {
                //console.info(data)
                if(data.code && data.code===401){
                    window.location.href = '/Login';
                }
                var menu="";
                var allData=data;

                //查看是否有缺省根页面
                var baseNodes = $.grep(data,function (node){
                    return node.level==0;
                });
                var matchs = $.grep(baseNodes,function (node){
                    return node.default_select==1;
                });
                var selectNode = null;
                if(matchs.length==0){
                    //无缺省页面,选择第一个
                    if(baseNodes.length>0){
                        selectNode = baseNodes[0];
                    }
                }else{
                    selectNode = matchs[0];
                }
                console.info(selectNode);

                for(var i = 0;i<data.length;i++){

                    if(data[i].level == 0){ //取出父元素的菜单，拼进页面
                        //查找子节点是否有选择的，有选择则父元素要展开，否则不展开
                        var node=data[i];
                        var childs=$.grep(allData,function (item) {
                            return node.id==item.pid;
                        });
                        if(childs.length>0){
                            var selected_childs=$.grep(childs,function (item) {
                                return item.default_select==1;
                            });

                            if(selected_childs.length>0){
                                menu += "<li data-name=\""+node.id+"\" class=\"layui-nav-item layui-nav-itemed\">";
                            }else {
                                menu += "<li data-name=\""+node.id+"\" class=\"layui-nav-item\">";
                            }

                            var root_href="";
                            if (node.url){
                                root_href+="lay-href=\""+node.url+"\"";//一级菜单配置了url则表示无子节点，需打开页面
                            }
                            menu += "<a href=\"javascript:;\" "+root_href+" lay-tips=\""+node.name+"\" lay-direction=\"2\"> <i class=\"layui-icon "+node.icon+"\"></i> <cite>"+node.name+"</cite> </a>";
                            for(var j = 0;j<childs.length;j++){ //继续遍历这几条数据
                                var default_select="";
                                if(childs[j].default_select===1){
                                    default_select+="class=\"layui-this\"";
                                }
                                menu +=    "<dl class='layui-nav-child'>";
                                menu +=        "<dd "+default_select+" data-name=\""+childs[j].id+"\">";
                                menu +=            "<a lay-href=\".."+childs[j].url+"\">"+childs[j].name+"</a>";
                                menu +=        "</dd>";
                                menu +=    "</dl>"
                            }
                            menu +=    "</li>";
                        }
                        else{
                            //
                            var default_select = false;
                            if(node.id==selectNode.id){
                                console.info("默认选中"+node.name);
                                console.info(node.url);
                                default_select = true;
                            }
                            //改节点为根节点，无子节点
                            var selected=default_select?"layui-this":"";
                            menu += "<li data-name=\""+node.id+"\" class=\"layui-nav-item  "+selected+" \" >";
                            var root_href="";
                            if (node.url){
                                root_href+="lay-href=\".."+node.url+"\"";//一级菜单配置了url则表示无子节点，需打开页面
                            }
                            menu += "<a href=\"javascript:;\" "+root_href+" lay-tips=\""+node.name+"\" lay-direction=\"2\"> <i class=\"layui-icon "+node.icon+"\"></i> <cite>"+node.name+"</cite> </a>";
                            menu +=    "</li>";

                            //src
                            if(default_select){
                                $("#main-iframe").attr("src",".."+node.url);
                            }

                        }
                    }

                }
                $("#LAY-system-side-menu").html(menu);
                var element = layui.element;
                element.init()//初始化element事件，使菜单展开
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载菜单失败');
            }

        });

        $.ajax({
            type: 'GET',
            url: '../Api/User',
            data: {},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (data) {
                //console.info(data)
                window.localStorage.setItem("user_info",JSON.stringify(data))
                if(data.name){
                    $("#username").html(data.name);
                    setWaterMark(data.name, data.phone)
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载用户信息失败');
            }
        });

        refreshAlarm();
        setInterval(function () {
            refreshAlarm();
        },60*1000);

        setInterval(function () {
            checkToken();
        },2*1000);

        

        //示范一个公告层
        // layer.open({
        //     type: 1
        //     ,title: false //不显示标题栏
        //     ,closeBtn: false
        //     ,area: '300px;'
        //     ,shade: 0.8
        //     ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        //     ,btn: [ '我已知晓']
        //     ,btnAlign: 'c'
        //     ,moveType: 1 //拖拽模式，0或者1
        //     ,content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;"><i class="layui-icon layui-icon-tips layui-text-orange" style="padding-right:10px;"></i><span class="layui-text-orange" style="font-size:20px;font-weight:700;">平台通知</span><br><br> 平台将于<br><span class="layui-text-orange">2024-07-14 星期天 凌晨0点到6点</span><br>进行平台升级,届时平台网页端和App客户端将不可以使用状态. 升级完成后即可正常使用。<br><br>智能饲喂器和网关终端设备功能不受影响(升级时间段无法下发方案等相关操作)，请各位知悉。</div>'
        //     ,success: function(layero){
                
        //     }
        // });

    });

    function refreshAlarm(){
        $.ajax({
            type: 'GET',
            url: '../Api/Alarm/Happen/Num',
            data: {},
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
            },
            dataType: "json",
            success: function (res) {
                //console.info(res);
                var ele = $("a[lay-href='../Alarm/Now']");
                console.info(ele);
                if(res.code>=0){
                    var num = res.count;
                    if(num>0){
                        if(num>99){
                            ele.html("当前告警<span class=\"layui-badge\">99+</span>");
                        }else{
                            ele.html("当前告警<span class=\"layui-badge\">"+num.toString()+"</span>");
                        }

                    }else{
                        ele.html("当前告警");
                    }
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg('加载告警信息失败');
            }
        });
    }

    function initToastr(){
        toastr.options = {
            "closeButton": false,
            "debug": false,
            "positionClass": "toast-bottom-right",
            "onclick": function(e) { layui.index.openTabsPage("/Alarm/AlarmCurrent", "<i class=\"layui-icon layui-icon-tips\"></i>"); },
            "showDuration": "300",
            "hideDuration": "1000",
            "timeOut": "5000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut",
        }
    }

    //检查token是否过期
    function checkToken(){
        var screenAlive,tokenAlive;

        
        //对比screenTimeouts 是否超时
        const timeout = 60 *60 * 1000; // 1小时
        var lastActivityTime = window.localStorage.getItem("lastActivityTime");
        if (Date.now() - lastActivityTime > timeout) {
            console.info("screen timeout");
            screenAlive = false;
        }
        else{
            screenAlive = true;
            //console.info("screen alive");
        }

        var datetime = window.localStorage.getItem("expire");
        //使用moment 对比datetime是否超过当前时间,datetime格式为 2025-07-08T09:45:58+08:00
        //console.info(datetime);
        datetime = moment(datetime).format("YYYY-MM-DD HH:mm:ss");
        //提前5s
        datetime = moment(datetime).subtract(10, 's');
        //console.info(datetime);
        if(moment(datetime).isBefore(moment())){
            tokenAlive = false;
        }else{
            tokenAlive = true;
            //console.info("token alive");
        }

        if(!screenAlive){
            window.localStorage.removeItem("token");
            window.localStorage.removeItem("expire");
            window.localStorage.removeItem("user_info");
            window.localStorage.removeItem("screenTimeout");
            window.location.href = "/Login";
        }
        else if(screenAlive && !tokenAlive){
            //续签token
            console.info("token expired, renew token");
            $.ajax({
                    type: 'POST',
                    url: '/Api/RefreshToken',
                    data: {},
                    beforeSend: function(request) {
                        request.setRequestHeader("Authorization", 'Bearer '+window.localStorage.getItem("token"));
                    },
                    dataType: "json",
                    success: function (res) {
                        layer.closeAll('loading');
                        if(res.code == 200){
                            //写入expire
                            var token = res.token;
                            var expire = res.expire;
                            window.localStorage.setItem("token",token);
                            window.localStorage.setItem("expire",expire);
                        }else if (res.code == 401){
                            layer.msg('登录信息已过期,请重新登录');
                            //window.localStorage.removeItem("token");
                            //window.localStorage.removeItem("expire");
                            //window.localStorage.removeItem("user_info");
                            //window.localStorage.removeItem("screenTimeout");
                            //window.location.href = "/Login";
                        }
                    },
                    dataType: "json"
                });
        }
        else{   
            console.info("token and screen alive");
        }
    }
</script>
<script src="../js/check_screen.js?v=${.Config.basic.version}"></script>

</body>

</html>